// import Vue from 'vue'
import { useUserStore } from '@/store/user'

// 检测是否有权限
export const permission = {
  mounted(el, binding) {
    const { value } = binding
    const userStore = useUserStore()

    const permissionList = userStore?.permissionList || []
    if (!checkKey(permissionList, value)) {
      // el.style.display = 'none'
      el.parentElement && el.parentElement.removeChild(el)
    }
  },
}

// 检测传入的元素key是否可以显示
function checkKey(columns, key) {
  // 如果传入的元素key不在权限数组里，则不可显示
  if (typeof key === 'string') {
    return columns.some((i) => i.resourceKey === key)
  } else if (Array.isArray(key)) {
    return columns.some((i) => key.includes(i.resourceKey))
  } else {
    return true
  }
}
